/*---------------------------------------------------------
   Name: SetPlayerInfo(pl)
   Desc: saves player information
---------------------------------------------------------*/

local dbtable = "mydb"

function RP08.SetPlayerInfo(pl)

	if not(pl:IsValid())then
		return
	end

	if not(pl:GetTable().gotinfo == true)then
		print("trying to save without loading")
		return ""
	end
	
	if(pl:GetNWInt( "Money" ) == 600)then
		return ""
	end
	
	local money = pl:GetNWInt( "Money" )
	local nick = database_server:escape(pl:Nick()) // Remember, always escape ANY field that a user has control over, lets not have your database comprimised now.
	local inventorycsv = ""
	
	for k, v in pairs(pl:GetTable().Inventory)do
	
		inventorycsv = inventorycsv .. k .. "=" .. v ..","
		
	end
	
	if(string.len( inventorycsv ) >= 300)then
		print("WARNING INVENTORY LENGTH MAXED!!!!")
	end
	
	local query = "UPDATE `"..Schema.."`.`"..dbtable.."` SET `Rpinventory` = '" .. inventorycsv .. "', `Money` = '" .. money .. "',`Name` = '"..nick.."' WHERE `SteamID` = '" .. pl:SteamID() .. "' LIMIT 1 ;"
	
	local SaveQuery = database_server:query(query)
	SaveQuery.onSuccess = function() end
	SaveQuery.onFailure = function(Q,F) print("###QUERY FAILED### ERROR: "..F) end
	SaveQuery.onError = function(Q,F) print("###ERROR IN QUERY### ERROR: "..F) end
	SaveQuery:start()
end


/*---------------------------------------------------------
   Name: RP08.GetPlayerInfo(pl)
   Desc: Loads the player info
---------------------------------------------------------*/
function RP08.GetPlayerInfo(pl)
	if not(pl:IsValid())then
		return
	end
	
	local LoadPlInfo = database_server:query("SELECT `Money`,`Rpinventory` FROM `"..dbtable.."` WHERE `SteamID` = '" .. pl:SteamID() .. "'")
	LoadPlInfo.pl = pl
	LoadPlInfo.onSuccess = RP08.ReturnQuery
	LoadPlInfo.onFailure = function(Q,F) print("###QUERY FAILED### ERROR: "..F) end
	LoadPlInfo.onError = function(Q,F) print("###ERROR IN QUERY### ERROR: "..F) end
	LoadPlInfo:start()
end

------------------------
-------Return of query user, sets the info from database
------------------------
function RP08.ReturnQuery(self)

	self.data = self:getData()
	if self.data[1]==nil then
		print("New Profile for "..self.pl:Nick().." is currently being created..")
		RP08.NewUser(self.pl)
		return
	end
	print(self.pl:Nick().."'s data has been loaded!")
	
	 self.pl:SetNWInt( "Money", tonumber(self.data[1].Money))
	
	local invcsv = string.Explode( "," , self.data[1].Rpinventory)
	self.pl:GetTable().Inventory = {}
		for k, v in pairs(invcsv)do
		 if (v or not v == "" or not v == nil)then
				local itam = string.Explode( "=" , v)
				self.pl:GetTable().Inventory[itam[1]] = tonumber(itam[2])
			end
		end
		RP08.SendPlayerInventory(self.pl)
	
	self.pl:GetTable().gotinfo = true
end

--------------------
--If a user didn't exist we add one
--------------------

function RP08.NewUser(pl)

	 local SteamID = pl:SteamID()
	 local Name = database_server:escape(pl:Nick()) // Remember, always escape ANY field that a user has control over, lets not have your database comprimised now.
		
	local NewUser = database_server:query("INSERT INTO `"..dbtable.."` (`SteamID`, `Name`)VALUES ('" .. SteamID .. "', '" .. Name .. "');")
	NewUser.pl = pl
	NewUser.onSuccess = function()
								print("New user created!")
								pl:GetTable().gotinfo = true
								print("Loading their information for the first time..")
								RP08.GetPlayerInfo(pl)
								end
	NewUser.onFailure = function(Q,F) print("###QUERY FAILED### ERROR: "..F) end
	NewUser.onError = function(Q,F) print("###ERROR IN QUERY### ERROR: "..F) end
	NewUser:start()
end

